package com.xq.chrome.web.controller;


import com.xq.chrome.web.entity.UserVo;
import com.xq.chrome.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * 用户的总入口
 *
 * @author MACHENIKE
 */
@Configuration
@EnableAutoConfiguration
@RestController
@RequestMapping("user")
public class UserController {

  @RequestMapping("/ping")
  public String ping() {
    return "ok1";
  }

  /**
   * 翻译文档
   *
   * @param url
   * @param request
   * @param response
   * @return
   * @throws IOException
   */
  @RequestMapping("/list")
  public List doWork(@RequestParam("url") String url, HttpServletRequest request, HttpServletResponse response) throws IOException {
    return null;
  }


  @Autowired
  UserService userService;

  @RequestMapping("/showuser.html")
  public ModelAndView showUserInfo(Integer id) {
    ModelAndView view = new ModelAndView();
    UserVo user = userService.getUser(id);
    view.addObject("user", user);
    view.setViewName("/userInfo");
    return view;
  }


  @GetMapping("/get/{id}.json")
  public @ResponseBody
  UserVo getUserInfo(@PathVariable Integer id) {
    UserVo user = userService.getUser(id);
    System.out.println(user);
//    user=new UserVo();
//    user.setAccount("1820449052");
    return user;
  }

  @GetMapping("/add")
  public @ResponseBody
  boolean getUserInfo(UserVo user) {
    boolean result = userService.addUser(user);
    return result;
  }

  @Autowired
  private DiscoveryClient discoveryClient;

  @GetMapping("/seeInfo")
  public @ResponseBody
  List<ServiceInstance> showInfo() {
    List<ServiceInstance> list = this.discoveryClient.getInstances("provider");
    return list;
  }


  /**
   * 测试微服务调用
   *
   * @param name
   * @return
   */
  @RequestMapping(value = "hello1", method = RequestMethod.GET)
  public String hello(@RequestParam String name) {
    return "hello" + name;
  }

  /**
   * 测试微服务调用
   *
   * @param name
   * @param age
   * @return
   */
  @RequestMapping(value = "hello2", method = RequestMethod.GET)
  public UserVo hello(@RequestParam String name, @RequestHeader Integer age) {
    UserVo userVo = new UserVo();
    return userVo;
  }

  /**
   * 测试微服务调用
   *
   * @param userVo
   * @return
   */
  @RequestMapping(value = "hello3", method = RequestMethod.POST)
  public String hello(@RequestBody UserVo userVo) {
    return "hello" + userVo.getAccount() + "," + userVo.getCreateDate();
  }
}
